package cn.neusoft.controller;

import cn.neusoft.domain.Dept;
import cn.neusoft.domain.Substation;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.query.DeptQuery;
import cn.neusoft.query.SubstationQuery;
import cn.neusoft.service.SubstationService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/substation", produces = "application/json;charset=utf-8")
@Api(value = "分站的api", description = "分站相关的CRUD功能")
public class SubstationController {

    @Autowired
    SubstationService substationService;

    @PostMapping
    @ApiOperation(value = "新增分站")
    public ResultBean add(@RequestBody Substation substation) {

        if (substation == null
                || substation.getParent_id() == null
                || StringUtils.isEmpty(substation.getSubstation_name())
                || StringUtils.isEmpty(substation.getStatus())
        ) {

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        substationService.add(substation);

        return ResultBean.success();
    }

    @PutMapping
    @ApiOperation(value = "修改分站信息")
    public ResultBean update(@RequestBody Substation substation) {

        if (substation == null
                || substation.getSubstation_id() == null || substation.getSubstation_id() == 0
                || substation.getParent_id() == null
                || StringUtils.isEmpty(substation.getSubstation_name())
                || StringUtils.isEmpty(substation.getStatus())
        ) {

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        substationService.update(substation);

        return ResultBean.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除分站")
    public ResultBean deleteById(@PathVariable("id") Long substation_id) {

        if (substation_id == null) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        substationService.deleteById(substation_id);

        return ResultBean.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询分站信息")
    public ResultBean findById(@PathVariable("id") Long substation_id) {

        if (substation_id == null) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Substation substation = substationService.findById(substation_id);

        return ResultBean.success(substation);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询所有分站的信息")
    public ResultBean findAll() {
        List<Substation> substationList = substationService.findAll();

        return ResultBean.success(substationList);
    }

    @PostMapping("/page")
    @ApiOperation("分页模糊查询分站信息")
    public ResultBean findByPage(@RequestBody SubstationQuery substationQuery) {

        if(substationQuery == null
                || StringUtils.isEmpty(substationQuery.getPageNow())
                || StringUtils.isEmpty(substationQuery.getPageSize())
        ){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        PageBean<Substation> pageBean = substationService.findByPage(substationQuery);

        return ResultBean.success(pageBean);
    }

    @GetMapping("/address")
    @ApiOperation("查询现有全部分站的地址（去重）")
    public ResultBean getDistinctAddress(){
        List<String> distinctAddress = substationService.getDistinctAddress();

        return ResultBean.success(distinctAddress);
    }


}
